import 'package:flutter/services.dart';
import 'package:flutter_base_library/index.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:safe_manage/config/my_app_data.dart';
import 'package:safe_manage/config/my_app_provider.dart';
import 'package:safe_manage/config/my_app_routes.dart';

class AlarmController extends GetxController {
  RefreshController refreshController = RefreshController();
  var chouyanValue = 0.obs;
  var dianziweilanValue = 0.obs;
  var phoneValue = 0.obs;
  var yanhuoValue = 0.obs;
  var zhuangxieyouValue = 0.obs;
  var jiayouzhan = 0.obs;
  var chedingValue = 0.obs;
  var renyuanValue = 0.obs;
  var shebeiValue = 0.obs;
  final initDataList = [
    AlarmTypeModel(
      title: "抽烟检测",
      img: "images/alarm/xiyan.png",
      url: "/safety-web/commonRecord/page",
      sjlx: "2",
      color: const Color(0xffec753a),
    ),
    AlarmTypeModel(
      title: "电子围栏检测",
      img: "images/alarm/dianziweilan.png",
      url: "/safety-web/intrusionRecord/page",
      sjlx: "6",
      color: const Color(0xff0b57a2),
    ),
    AlarmTypeModel(
      title: "电话检测",
      img: "images/alarm/dianhua.png",
      url: "/safety-web/commonRecord/page",
      sjlx: "8",
      color: const Color(0xffdd8790),
    ),
    AlarmTypeModel(
      title: "烟火检测",
      img: "images/alarm/yanhuo.png",
      url: "/safety-web/commonRecord/page",
      sjlx: "9",
      color: const Color(0xff59bcbc),
    ),
    AlarmTypeModel(
      title: "装卸油检查",
      img: "images/alarm/zhuanxieyou.png",
      url: "/safety-web/zhuangXieYouRecord/page",
      sjlx: "18",
      color: const Color(0xff59bcbc),
    ),
    AlarmTypeModel(
      title: "加油站巡检",
      img: "images/alarm/jiayouzhan.png",
      url: "/safety-web/patrolRecord/page",
      sjlx: "17",
      color: const Color(0xff0b57a2),
    ),
    AlarmTypeModel(
      title: "车顶作业",
      img: "images/alarm/cheding.png",
      url: "/safety-web/commonRecord/page",
      sjlx: "16",
      color: const Color(0xffec753a),
    ),
    AlarmTypeModel(
      title: "人员识别",
      img: "images/alarm/renyuan.png",
      url: "",
      sjlx: "4",
      color: const Color(0xffec753a),
    ),
    AlarmTypeModel(
      title: "设备状态",
      img: "images/alarm/shebei.png",
      url: "/safety-web/networkRecord/list",
      sjlx: "13",
      color: const Color(0xFF4095E5),
    ),
    AlarmTypeModel(
      title: "离岗检测",
      img: "images/alarm/ligang.png",
      url: "/safety-web/absenceRecord/page",
      sjlx: "20",
      color: const Color(0xFF4095E5),
    ),
  ];
  var dataList = <AlarmTypeModel>[].obs;
  @override
  void onInit() {
    super.onInit();
    getEventNum();
    Get.context?.read<MyAppProvider>().updateCount = () {
      getEventNum();
    };
  }

  void getEventNum() async {
    dataList.clear();
    int count = 0;
    for (var element in initDataList) {
      AlarmTypeModel model = AlarmTypeModel(
        title: element.title,
        img: element.img,
        url: element.url,
        sjlx: element.sjlx,
        color: element.color,
      );
      if (model.sjlx != "17") {
        await HttpUtils.get(
          "/safety-web/eventRecord/getWCLEventRecordNumber",
          query: {
            "cjyhid": MyAppData.instance.currentUser!.id.toString(),
            "sjlx": element.sjlx,
          },
          success: (result) {
            var data = result['data'];
            if (data is Map) {
              model.count = int.tryParse(data['tjsl'].toString()) ?? 0;
            }
          },
        );
      }
      count += model.count;
      dataList.add(model);
    }
    refreshController.refreshCompleted();
    Get.context?.read<MyAppProvider>().changeNotifyCount(count);
  }

  void listItemClicked(index) {
    if (index < 7) {
      Get.toNamed(
        MyAppRoutes.alarmTypeList,
        arguments: dataList[index],
        parameters: {
          "type": index.toString(),
          "sjlx": dataList[index].sjlx,
        },
      )?.then((value) {
        refreshController.requestRefresh();
      });
    } else if (index == 7) {
      Get.toNamed(
        MyAppRoutes.galleryFace,
      )?.then((value) {
        refreshController.requestRefresh();
      });
    } else if (index == 8) {
      Get.toNamed(
        MyAppRoutes.galleryDevice,
      )?.then((value) {
        refreshController.requestRefresh();
      });
    } else if (index == 9) {
      Get.toNamed(
        MyAppRoutes.alarmTypeList,
        arguments: dataList[index],
        parameters: {
          "type": index.toString(),
          "sjlx": dataList[index].sjlx,
        },
      )?.then((value) {
        refreshController.requestRefresh();
      });
    }
  }
}

class AlarmTypeModel {
  final String title;
  final String img;
  final String url;
  final String sjlx;
  final Color color;
  int count = 0;
  AlarmTypeModel({
    required this.title,
    required this.img,
    required this.url,
    required this.sjlx,
    required this.color,
  });
}
